<?php

App::uses('AppController', 'Controller');

class IndividualsController extends AppController
{
    public $name = 'Individuals';
    public $uses = array('Individual');
    
    // Páginas do CMS AQUI
    function index()
    {
        
        $Individuals = $this->Individual->find('all', array('order'=>'name ASC'));
        
        //die(debug($this->Provider->getEnumValues('activity')));
        
        $this->set('Individuals', $Individuals);
    }

    function add()
    {
       
        $individual_locality = $this->Individual->getEnumValues('locality');
        $this->set('individual_locality', $individual_locality);
        
        
        $enviar = false;
        
        
        if($this->request->is('post'))
        {
            if(empty($this->request->data['Individual']['cpf']))
            {
                $enviar = true;   
            }
            else
            {
                if($this->VALIDACPF($this->request->data['Individual']['cpf'],$cpf) == 0)
                {
                    $envia = false;
                    $this->Session->setFlash('<div class="alert alert-error">CPF INVÁLIDO!</div>');
                }
                else
                {
                    $enviar = true;
                }
            }
            
            if($enviar)
            {         

                if ($this->Individual->save($this->request->data))
                {
                    // Set a session flash message and redirect.
                    $this->Session->setFlash('<div class="alert alert-success">Cdastrado com Sucesso!</div>');

                }
            }
        }
    }
    function delete($id=null)
    {
        $this->Individual->delete($id);
        $this->Session->setFlash('<div class="alert alert-success"><div class="alert alert-success">Removido com sucesso!</div>');
        $this->redirect(array('action' => 'index'));
    }
    
    function edit($id)
    {
        $individual_locality = $this->Individual->getEnumValues('locality');
        $this->set('individual_locality', $individual_locality);
       
        if($this->data)
        {
//            if(!isset($this->request->data['Provider']['type'])){$this->request->data['Page']['type'] = 0;}
            
            if ($this->Individual->save($this->request->data))
            {
                // Set a session flash message and redirect.
                $this->Session->setFlash('<div class="alert alert-success">Cdastrado com Sucesso!</div>');
                
            }
         }
         else
         {
            $this->data = $this->Individual->read(null,$id);
         }
        
        
    }
}
